package xmltomce;
import java.io.File;


public class Movie {
	private File dvdFolder;
	private int xmlId;
	private XPath xmlData;
	
	private String director;
	private String actors;
	private String genre;
	private String releaseDate;
	private int runtimeInMinutes = 0;
	private String plot;
	private String largeCoverPath;
	private String smallCoverPath;
	
	private String movieRootQuery;
	
	
	public Movie(File dvdFolder, XPath xmlData){
		this.dvdFolder = dvdFolder;
		this.xmlData = xmlData;
		this.xmlId = xmlData.getNodeIntValue("movie/id");
		movieRootQuery = "movie/";
	}
	
	public String getTitle(){
		return dvdFolder.getName();
	}
	
	public String getMovieRootQuery(){
		return movieRootQuery;
	}
	
	public String getDirector(){
		if (director == null){
			String query = getMovieRootQuery()+"/crew/crewmember[roleid='dfDirector']/person/displayname";
			director = xmlData.getNodeListAsText(query, ';');
		}
		return director;
	}
	
	public String getActors(){
		if (actors == null){
			String query = getMovieRootQuery()+"/cast/star[roleid='dfActor']/person/displayname";
			actors = xmlData.getNodeListAsText(query, ';');
		}
		return actors;
	}
	
	public String getGenre(){
		if (genre == null){
			String query = getMovieRootQuery()+"/genres/genre/displayname";
			genre = xmlData.getNodeListAsText(query, ';');
		}
		return genre;
	}
	
	public String getReleaseDate(){
		if (releaseDate == null){
			releaseDate = xmlData.getNodeText(getMovieRootQuery()+"/releasedate/year/displayname");
			releaseDate += " 01 01"; // pseudo Month & Day for MCE compatibility
		}
		return releaseDate;
	}
	
	public int getRuntimeInMinutes(){
		if (runtimeInMinutes == 0){
			String runtime = xmlData.getNodeText(getMovieRootQuery()+"/runtime");
			String[] splitted = runtime.split(" ");
			if (splitted.length > 1){
				runtimeInMinutes = Integer.parseInt(splitted[0]);
			}
		}
		return runtimeInMinutes;
	}
	
	public String getPlot(){
		if (plot == null){
			plot = xmlData.getNodeText(getMovieRootQuery()+"/plot");
		}
		return plot;
	}
	
	public String getLargeCoverPath(){
		if (largeCoverPath == null){
			largeCoverPath = xmlData.getNodeText(getMovieRootQuery()+"/coverfront");
		}
		return largeCoverPath;
	}
	
	public String getSmallCoverPath(){
		if (smallCoverPath == null){
			smallCoverPath = xmlData.getNodeText(getMovieRootQuery()+"/thumbfilepath");
		}
		return smallCoverPath;
	}

	public File getDvdFolder() {
		return dvdFolder;
	}

	public void setDvdFolder(File dvdFolder) {
		this.dvdFolder = dvdFolder;
	}

	public int getXmlId() {
		return xmlId;
	}

	public void setXmlId(int xmlId) {
		this.xmlId = xmlId;
	}
	
	@Override
	public String toString(){
		return dvdFolder+" ("+xmlId+")";
	}

}
